package com.atguigu.stack;

public class ArrayStackDemo {

}

class ArrayStack{
    private int maxSize;//栈的大小
    private int[] stack;//数组模拟栈
    private int top=-1;//表示栈顶，初始化为1
    //构造器
    public ArrayStack(int size){
        this.maxSize=size;
        stack=new int[size];
    }
    //栈满
    public boolean isFull(){
        return top==maxSize-1;
    }
    //栈空
    public boolean isEmpty(){
        return top==-1;
    }
    //入栈
    public void push(int n){
        if(isFull()){
            System.out.println("栈满了，无法继续添加了");
            return;
        }
        stack[++top]=n;
    }
    //出栈
    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("栈空，没有数据可以取出");
        }
        return stack[top--];
    }
    //显式栈
    public void list(){
        if(isEmpty()){
            System.out.println("栈空，没有数据可以取出");
        }
        while (top!=-1){
            System.out.printf("top%d 为%d ",top, stack[top--]);
        }
    }
}